<template>
  <div class="jeecg-basic-table gete-log-view">
    <div class="tip flex items-center head-title">
      <div class="flex items-center head-title">
        <ArrowLeftOutlined @click="onBack" />
        <span class="name">门闸操作日志列表</span>
      </div>
      <span class="tip-img"></span>
    </div>
    <div class="jeecg-search-layer">
      <a-form ref="form" :model="queryParam">
        <a-row :gutter="[16, 16]">
          <a-col :span="8">
            <a-form-item label="操作时间">
              <a-range-picker
                v-model:value="dateTime"
                format="YYYY-MM-DD HH:mm:ss"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
                show-time
                allowClear
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="操作人员">
              <a-select v-model:value="queryParam.operator" style="width: 100%" allowClear>
                <template v-for="(vo, idx) in userList" :key="idx">
                  <a-select-option :value="vo.username">
                    {{ vo.realname }}
                  </a-select-option>
                </template>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24" style="text-align: right">
            <a-form-item>
              <a-button type="primary" @click="onSearch">查询</a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="jeecg-table-view">
      <a-table :loading="loading" rowKey="id" :dataSource="dataSource" :columns="columns" :pagination="false">
        <template v-slot:bodyCell="{ column, record, index }">
          <template v-if="column.dataIndex == 'action'">
            <a @click="onAdd(record)">编辑</a>
          </template>
        </template>
      </a-table>
      <Pagination
        v-model:current="ipagination.current"
        :pageSize="ipagination.pageSize"
        :total="ipagination.total"
        :showTotal="ipagination.showTotal"
        show-less-items
        show-quick-jumper
        @change="handlePageChange"
        @showSizeChange="onShowSizeChange"
        :hideOnSinglePage="true"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch, getCurrentInstance, onMounted, nextTick } from 'vue';
import { BasicColumn } from '/@/components/Table';
import { ArrowLeftOutlined } from '@ant-design/icons-vue';
import { Pagination } from 'ant-design-vue';
import { entranceLogApi } from '/@/api/gate/index';
import { stationListApi } from '/@/api/station/index';
import { listNoCareTenant } from '/@/views/system/user/user.api';

let { proxy } = getCurrentInstance();

let loading = ref(false);

let queryParam = ref({
  entranceCode: '',
  // 操作时间开始
  operateTimeStart: '',
  // 操作时间结束
  operateTimeEnd: '',
  // 操作人员
  operator: '',
});

let dateTime = ref([]);

watch(
  () => dateTime.value,
  (date) => {
    if (date) {
      if (date.length !== 0) {
        queryParam.value.operateTimeStart = date[0];
        queryParam.value.operateTimeEnd = date[1];
      }
    } else {
      queryParam.value.operateTimeStart = '';
      queryParam.value.operateTimeEnd = '';
    }
  }
);

// 数据源
let dataSource = ref([]);

let userList = ref([]);

let columns: BasicColumn[] = [
  {
    title: '站点名称',
    align: 'center',
    dataIndex: 'stationCode',
    customRender: ({ text }) => {
      return stationList.value.find((_) => _.stationCode === text)?.name;
    },
  },
  {
    title: '门闸编码',
    align: 'center',
    dataIndex: 'entranceCode',
  },
  {
    title: '操作时间',
    align: 'center',
    dataIndex: 'operateTime',
  },
  {
    title: '开关动作',
    align: 'center',
    dataIndex: 'operateTo',
    customRender: ({ text }) => {
      let config = {
        1: '开门',
        2: '关门',
        3: '暂停',
      };
      return config[text];
    },
  },
  {
    title: '操作人',
    align: 'center',
    dataIndex: 'operator',
    customRender: ({ text }) => {
      return userList.value.find((_) => _.username === text)?.realname;
    },
  },
];

// 站点列表
let stationList = ref([]);

let ipagination = ref({
  current: 1,
  pageSize: 10,
  pageSizeOptions: ['10', '20', '30'],
  showTotal: (total, range) => {
    return range[0] + '-' + range[1] + ' 共' + total + '条';
  },
  showQuickJumper: true,
  showSizeChanger: true,
  total: 0,
});

/**
 * 初始化门闸操作日志列表
 */
function onInit() {
  let { equipmentCode } = proxy.$route.query;
  queryParam.value['equipmentCode'] = equipmentCode;
  let { current, pageSize } = ipagination.value;
  let data = {
    current,
    size: pageSize,
  };
  let keys = Object.keys(queryParam.value);
  Object.values(queryParam.value).map((_, i) => {
    if (_ !== '') {
      data[keys[i]] = _;
    }
  });
  loading.value = true;
  entranceLogApi(data).then((res) => {
    loading.value = false;
    let { records, size, current, total } = res;
    Object.assign(ipagination.value, {
      current,
      pageSize: size,
      total,
    });
    dataSource.value = records;
  });
}

/**
 * 切换当前页数
 */
function handlePageChange(page) {
  ipagination.value.current = page;
  onInit();
}

function onShowSizeChange(current, pageSize) {
  Object.assign(ipagination.value, { current, pageSize });
  onInit();
}

/**
 * 搜索数据
 */
function onSearch() {
  ipagination.value.current = 1;
  onInit();
}

/**
 * 获取用户列表
 */
function getUserList() {
  let data = {
    pageNo: 1,
    pageSize: 10000000,
  };
  listNoCareTenant(data).then(({ records }) => {
    userList.value = records;
  });
}

function onBack() {
  proxy.$router.go(-1);
}

onMounted(async () => {
  await getUserList();
  stationList.value = await stationListApi({});
  onInit();
});
</script>

<style lang="less" scoped>
.gete-log-view {
  margin: 12px;
  background: #fff;
  .tip {
    width: 100%;
    height: 44px;
    padding: 0 0 0 43.3px;
    background: url('@/assets/images/img-17.png') no-repeat;
    background-size: 100% 100%;
    line-height: 44px;
    .head-title {
      .name {
        margin: 0 0 0 12px;
        font-size: 16px;
        color: #fff;
      }
    }
    span {
    }
  }
  .jeecg-search-layer {
    margin: 0 0 17px;
    padding: 12px 15px;
    background: #fff;
    border-radius: 8px;
    .ant-form {
      max-width: auto;
      .ant-form-item {
        margin-bottom: 24px;
      }
      :deep(.ant-col) {
        width: auto;
      }
    }
  }
  .jeecg-table-view {
    margin: 24px;
    .card-list {
      padding: 0 0 24px;
    }
    .ant-pagination {
      padding: 12px 0;
    }
  }
}
</style>